/**
 * @file files.c
 * @brief File functions.
 * @version 1.0
 * @date Out-2011
 * @author Leonardo Lino 2101101
 * @author Joaquim Ferreira 2101104
 */

#include <ctype.h>
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "debug.h"
#include "files.h"
#include "memory.h"

/** @brief ERR_IO 1 - A constant that means that an error occurred.*/
#define ERR_IO 1

/** @brief This function opens a file and check for errors.*/
FILE *my_fopen (const char *filename, const char *mode)
{
  FILE *ptr = fopen (filename, mode);

  G_proc_files++;

  if (ptr == NULL) {
    ERROR (ERR_IO, "Unable to open the file %s.\n", filename);
    G_proc_files--;
  }
  return ptr;
}

/** @brief Function that verifies if the filename has the termination ".xxx" eg. ".pdf" or ".txt".*/
int check_file_type (char *filename, char *type)
{
  /**< @brief Get the lenght of the filename string.*/
  int file_len = strlen (filename);
  /**< @brief Get the lenght of the type string.*/
  int type_len = strlen (type);

  /**< @brief Check if the string has the termination type ".xxx".*/
  if (filename[file_len - 4] == type[type_len - 4]
      && filename[file_len - 3] == type[type_len - 3]
      && filename[file_len - 2] == type[type_len - 2] 
	  && filename[file_len - 1] == type[type_len - 1])
    return 1; /**< @brief True, return 1.*/
  else
    return 0; /**< @brief False, return 0.*/
}

/** @brief Function that convert the pdf file to txt file using the pdftotext aplication.*/
char *convert_pdf (char *filename)
{
  char *filetxt = NULL;
  char *sysc = NULL;
  int err;
  /**< @brief Get the lenght of the string.*/
  int len = strlen (filename);

  /**< @brief filetxt has the same length that filename.*/
  filetxt = (char *) MY_MALLOC (sizeof (char) * len);
  /**< @brief Copy the filename string without the extension ".xxx".*/
  strncpy (filetxt, filename, (len - 4));
  /**< @brief Adds the extention ".txt". */
  strcat (filetxt, ".txt");
  /**< @brief Will contain the command to use in the system function.*/
  sysc = (char *) MY_MALLOC (sizeof (char) * (2 * len + 12));
  /**< @brief Create the string "pdftotext filename.pdf filename.txt".*/
  strcpy (sysc, "pdftotext ");
  strcat (sysc, filename);
  strcat (sysc, " ");
  strcat (sysc, filetxt);
  /**< @ Call the aplication to convert with the right parameters.*/
  if ((err = system (sysc)) == -1) {
      /**< @brief An error occurred. */
    printf ("Unable to convert pdf file to text.\n");

    MY_FREE (filetxt);
    MY_FREE (sysc);
    return NULL;
  }
  
  MY_FREE (sysc);
  return filetxt;
}

/** @brief Function that will remove the created text file that was convert from the pdf file.*/
void rem_filetxt (char *filetxt)
{
  char *sysc = NULL;

  /**< @brief sysc will contain the command plus the filetxt to use in the system function "rm".*/
  sysc = (char *) MY_MALLOC (sizeof (char) * (strlen (filetxt) + 4));
  /**< @brief Create the string "rm filename.txt".*/
  strcpy (sysc, "rm ");
  strcat (sysc, filetxt);
  /**< @ Call the system command to remove the file text.*/
  system (sysc);
  MY_FREE (sysc);
}